package com.sky.controller.admin;

import com.sky.properties.AliyunConfigProperties;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description uploadController
 * @Author luohai
 * @Date 2024-09-30
 */
@RestController
@RequestMapping("/admin/common/upload")
public class UploadController {

    @Autowired
    private AliyunConfigProperties aliyunConfigProperties;

    @PostMapping
    public Result fileUpload(MultipartFile file) throws Exception {
        //获取图片后缀名
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String bucketName = originalFilename.substring(i);
        //调用Aliyun工具类
        String upload = AliyunOSSUtils.upload(aliyunConfigProperties.getEndpoint(),
                aliyunConfigProperties.getBucketName(),
                file.getBytes(),
                bucketName);
        return Result.success(upload);
    }
}
